package com.liangwei.jd.service;

import com.liangwei.jd.vo.LoginResult;
import com.liangwei.jd.vo.UserInfoVo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.GregorianCalendar;

@Component
public class MaoTaiService {
    private static final Logger log = LoggerFactory.getLogger(MaoTaiService.class);
    @Value("${driver}")
    private String driverUrl;

    @Autowired
    private LoginService loginService;

    public void start() {

        ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().usingDriverExecutable(new File(driverUrl)).usingAnyFreePort().build();

        try {
            chromeDriverService.start();

            ChromeDriver chromeDriver = new ChromeDriver(chromeDriverService);
            chromeDriver.get("https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fplus.jd.com%2Findex");

            WebDriverWait wait = new WebDriverWait(chromeDriver, 30);
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setUsername("wuqi5123");
            userInfoVo.setPassword("wuqih5123");
            LoginResult loginResult1;
            while (1 == 1) {
                loginResult1 = loginService.login(chromeDriver, userInfoVo);
                if (loginResult1.isSuccess()) {
                    break;
                }

            }
            //上午 https://item.jd.com/100012043978.html
            GregorianCalendar ca = new GregorianCalendar();
            if (ca.get(GregorianCalendar.AM_PM) == 0) {

                //chromeDriver.get("https://item.jd.com/100012043978.html");
            } else {
                chromeDriver.get("https://item.jd.com/100014530230.html?yuyue=0");
            }


            WebDriverWait wait2 = new WebDriverWait(chromeDriver, 1000);


            for (int i = 0; i < 10; i++) {
                chromeDriver.findElementByXPath("//*[@id=\"choose-btn-ko\"]").click();

            }
//   if(chromeDriver.getCurrentUrl())

            //chromeDriver.get("https://item.jd.com/100012043978.html");
            chromeDriver.quit();
            chromeDriverService.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
